package util;

import com.sun.jdi.Value;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.Key;
import java.util.Properties;

public class ProPerTies {
    /**
     * 程序化
     * @param key
     * @param Value
     * @param path
     */
    public void properties(Properties properties ,String path,String comments){
        FileOutputStream fo=null;
        try {
            fo=new FileOutputStream(path);
            properties.store(fo,comments);
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        finally {
            if (fo!=null){
                try {
                    fo.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 反程序化
     * @param path
     * @return
     */
    public Properties properties1(String path){
        Properties ppt=new Properties();
        FileInputStream fi=null;
        try {
            fi=new FileInputStream(path);
            ppt.load(fi);
            return ppt;
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        } finally {
            if (fi!=null){
                try {
                    fi.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    public static void main(String[] args) {
        String url="http://www.lovo.com/index?name=aaaaaa&age=20&gender=男";
        Properties ppt=new Properties();
        String str=url.substring(url.indexOf('?')+1,url.length());
        String[] strArr=str.split("&");
        for (int i=0;i<strArr.length;i++){
            String[] StrArr=strArr[i].split("=");
            ppt.put(StrArr[0],StrArr[1]);
        }
        new ProPerTies().properties(ppt,"2.properties",null);
        ppt=new ProPerTies().properties1("2.properties");
        System.out.println(ppt.getProperty("age"));
    }
}
